﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Loader
{
    /// <summary>
    /// Interaction logic for patientListItem3.xaml
    /// </summary>
    public partial class patientListItem3 : UserControl
    {
        public patient selectedPatient;
        List<Image> jointList = new List<Image>();
        List<Button> jointBtnList = new List<Button>();
        ServiceReference2.Service1Client svc = new ServiceReference2.Service1Client();
        public patientListItem3(patient p, List<ServiceReference2.EXE> exeAssignedList, List<ServiceReference2.session> patientSessionList, List<ServiceReference2.problem> problemList)
        {
            InitializeComponent();

            # region add image into a list
            jointList.Add(joint_1);
            jointList.Add(joint_2);
            jointList.Add(joint_3);
            jointList.Add(joint_4);
            jointList.Add(joint_5);
            jointList.Add(joint_6);
            jointList.Add(joint_7);
            jointList.Add(joint_8);
            jointList.Add(joint_9);
            jointList.Add(joint_10);
            jointList.Add(joint_11);
            jointList.Add(joint_12);
            jointList.Add(joint_13);
            jointList.Add(joint_14);
            #endregion

            # region add button into a list
            jointBtnList.Add(btn_joint_1);
            jointBtnList.Add(btn_joint_2);
            jointBtnList.Add(btn_joint_3);
            jointBtnList.Add(btn_joint_4);
            jointBtnList.Add(btn_joint_5);
            jointBtnList.Add(btn_joint_6);
            jointBtnList.Add(btn_joint_7);
            jointBtnList.Add(btn_joint_8);
            jointBtnList.Add(btn_joint_9);
            jointBtnList.Add(btn_joint_10);
            jointBtnList.Add(btn_joint_11);
            jointBtnList.Add(btn_joint_12);
            jointBtnList.Add(btn_joint_13);
            jointBtnList.Add(btn_joint_14);
            # endregion

            selectedPatient = p;
            int age = new DateTime(DateTime.Now.Subtract(p.DateOfBirth).Ticks).Year - 1;

            addressLabel1.Address1 = p.Address1;
            addressLabel1.Address2 = p.Address2;
            DOBValue.Text = String.Concat(p.DateOfBirth.ToShortDateString(), " (", age.ToString(), ")");
            lbl_email.Text = p.Email;
            nameLabel1.FamilyName = p.LastName;
            if (p.Gender.ToLower().Equals("m"))
            {
                GenderValue.Text = Microsoft.Cui.Controls.PatientGender.Male.ToString();
            }
            else if (p.Gender.ToLower().Equals("f"))
            {
                GenderValue.Text = Microsoft.Cui.Controls.PatientGender.Female.ToString();
            }

            nameLabel1.GivenName = String.Concat(p.FirstName);
            lbl_mobile.Text = p.ContactNum;
            lbl_home.Text = p.ContactNumHome;
            nameLabel1.Name = p.FirstName;
            img_patient.Source = p.Image;
            addressLabel1.Postcode = p.Postcode;
            nameLabel1.Title = p.Title;
            IdentifierValue.Text = p.Uid.ToString();
            //contactLabel1.WorkPhoneNumber = p.ContactNumWork;
            addressLabel1.Country = p.Country;
            //lblEname1.Text = p.E_name;
            //lblEContact1.Text = p.E_contactNum;
            //lblErelation1.Text = p.E_relationship;
            this.exeAssignedList = exeAssignedList;

            // retrieve all treatment analysis data
            this.patientSessionList = patientSessionList;

            //retrieve problems that the patient have
            this.problemList = problemList;
            setPatientProblem();
        }

        // set problems of a patient (bad joint)
        private void setPatientProblem()
        {
            for (int i = 0; i < problemList.Count(); i++)
            {
                ServiceReference2.problem temp = problemList[i];
                int joint = Convert.ToInt16(temp.Joint);
                foreach (Image image in jointList)
                {
                    if (Convert.ToInt16(image.Tag) == joint)
                    {
                        // change the image
                        image.Source = new BitmapImage(new Uri("jointBad.png", UriKind.Relative));
                        image.MouseEnter += iBad_MouseEnter;
                        image.MouseLeave += iBad_MouseLeave;
                        image.Tag = temp.Uid;
                        continue;
                    }
                }
                //foreach (Button btn in jointBtnList)
                //{
                //    if (Convert.ToInt16(btn.Tag) == joint)
                //    {
                //        btn.Click += new RoutedEventHandler(joint_btn_Click);
                //        btn.Tag = temp.Uid;
                //        btn.ToolTip = temp.Description;
                //        continue;
                //    }
                //}
            }
            foreach (Image btn in jointList)
            {
                if (Convert.ToInt16(btn.Tag) <= 14)
                {
                    btn.Visibility = Visibility.Collapsed;
                }
            }
        }

        # region MouseEnter MouseLeave events
        void i_MouseLeave(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointFine.png", UriKind.Relative));
        }

        void i_MouseEnter(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointFineH.png", UriKind.Relative));
        }

        void iBad_MouseLeave(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointBad.png", UriKind.Relative));
        }

        void iBad_MouseEnter(object sender, MouseEventArgs e)
        {
            Image image = (Image)sender;
            image.Source = new BitmapImage(new Uri("jointBadH.png", UriKind.Relative));
        }
        #endregion
        public List<ServiceReference2.EXE> exeAssignedList = new List<ServiceReference2.EXE>();
        public List<ServiceReference2.session> patientSessionList = new List<ServiceReference2.session>();
        public List<ServiceReference2.problem> problemList = new List<ServiceReference2.problem>();
    }
}
